package com.vsoft.sms_cm;

import java.io.DataOutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.log4j.Logger;

import com.vsoft.framework.core.BaseException;
import com.vsoft.framework.core.BusiObj;
import com.vsoft.framework.core.Configuration;
import com.vsoft.framework.core.db.DBGadgets;
import com.vsoft.framework.core.db.DBTrans;
import com.vsoft.framework.core.db.OperAgent;
import com.vsoft.framework.core.db.SQLArgs;
import com.vsoft.framework.core.esb.ServiceBus;
import com.vsoft.framework.core.util.BOHelper;
import com.vsoft.framework.core.util.LogCollect;
import com.vsoft.framework.core.util.XMLUtil;
import com.vsoft.framework.server.servlet.SessionData;
import com.vsoft.framework.server.servlet.HttpUtil;
import com.vsoft.anti_fra.*;

public class RefreshCacheFromCallRecord {

	private static Logger vLogger = Logger.getLogger(RefreshCacheFromCallRecord.class);
	private static String vName = RefreshCacheFromCallRecord.class.getName();
	private SessionData vSD = null;
	private String vDebugHome = "";
	private BusiObj				vInBO		=  new BusiObj();
	
	
	public static void main(String[] args)  {
	
		try{

			vLogger.info("---------------InitSystem-------------");
			Configuration.InitSystem();
			vLogger.info("---------------generate test data-------------");
			Connection conn = DBTrans.getConnection();
			BusiObj bo=new BusiObj() ;
			TableCacheServer.setDebugFlag("1");
			String	sql	=	""
				+" select nvl(a.state,'0') state,nvl(a.wp_state,'0')  wp_state,a.ent_code ent_code,a.area_code area_code "
				+" ,b.called_num called_num,b.area_no area_no,b.phone_num phone_num " 
				+" from order_info a "
				+" ,(select substr(called_num,1,4)area_no, substr(called_num,5) phone_num ,called_num called_num " 
				+"  from(select distinct called_num called_num  "
				+"       from sms_call_record where sms_opdate>sysdate -1 and remark like '%没有配置短信模板%') "
				+"  )b "
				+" ,area c "
				+" where a.area_code=c.cat_code and a.vouch_number=b.phone_num and b.area_no=c.area_no ";
			vLogger.info("---------------获取需刷新数据的sql-------------:"+sql);
			String	RESULT = "RESULT";
			String	phone_num="";
			int 		iCnt	=	0,iRefreshCnt=0;
			SQLArgs 	Arg				= 	new SQLArgs();
			Arg.reset();
			BusiObj		refreshBo 	= OperAgent.execQuery(conn, "refresh phone number", sql, Arg,true);
			ArrayList<BusiObj> alRefresh			=	refreshBo.getBusiObjList(RESULT);
			for(BusiObj m:alRefresh)
			{
				phone_num = m.getString("phone_num");
				String state = m.getString("state");
				String wp_state = m.getString("wp_state");
				String ent_code = m.getString("ent_code");
				String area_code = m.getString("area_code");
				String called_num = m.getString("called_num");
				String area_no = m.getString("area_no");

				iCnt++;
				if(state.equals("0")&&wp_state.equals("0"))
				{
					String	refreshDesc	=	"号码:"+called_num+"没有有效的电话名片和旺铺业务的订购信息;" +
								" ----------------查询记录返回的xml："+BOHelper.boToXML(m);
					vLogger.info(refreshDesc);
					continue;
				}
				BusiObj	refreshEntBo	=	new BusiObj();
				refreshEntBo.setConnection(conn);
				refreshEntBo.setString("ent_code", ent_code);
				vLogger.info("--------------ent_code:"+ent_code );
				TableCacheServer.refreshCallNumber(refreshEntBo);
				iRefreshCnt++;
				bo.setString("order_num", called_num);
				bo.setString("mobile_num", "15305669020");
				bo.setString("cdr_type","1");
				bo.setConnection(conn);
				bo=TableCacheServer.getTempleForSend(bo);
				vLogger.info("测试刷新号码："+called_num+"返回数据是:\n" + BOHelper.boToXML(bo));
			}
			//TableCacheServer.init(conn);
			vLogger.info("总共检查"+iCnt+"个号码，实际刷新"+iRefreshCnt+"个号码" );
			conn.rollback();
			conn.close();
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
		catch(BaseException e)
		{
			e.printStackTrace();
			vLogger.error(e.ErrMethod);
			vLogger.error(e.SysMsg);
		}
			vLogger.info("---------------Game Over-------------");
	}	

		

	private int  init()
	{
		int iRet = 0;
		vSD = new  SessionData();
		vSD.vRoleCode = new String[] {"1"};
		vSD.vStaffCode = "1";
		vSD.vStaffNo = "0001";
		vSD.vStaffName = "test";
		
	
		vDebugHome = System.getProperty("DEBUG_HOME");
		if (vDebugHome == null)
		{
			iRet = -1;
			vLogger.error("需要设置DEBUG_HOME环境变量来指定debug 文件所在的目录");
		}
		return iRet;
		
	}
	
}
